﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SystemConfigurationManager.cs" company="http://mockingbird.codeplex.com">
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.IO;

namespace MockingBird.Common.Infrastructure.Configuration
{
    using System;
    using System.Configuration;
    using System.Diagnostics;
    using System.Threading;
    using SC = System.Configuration;

    /// <summary>
    /// Manages all the configuration for the MockingBird System.
    /// The primary purpose is to move any application configuration info
    /// outside of web.config. 
    /// when normal configuration sections are required, it behaves just 
    /// like the NetFx configuration manager and returns sections so the caller
    /// uses them in whatever manner they choose. Strongtyping these sections
    /// would then tightly couple the caller to this class. 
    /// </summary>
    public class SystemConfigurationManager
    {
        public SC.ConfigurationSectionGroup MockingBirdConfiguration { get; private set; }

        public SystemConfigurationManager()
        {
            Debug.WriteLine(string.Format("ConfigFileBasedHandlerResolver.ctor on thread ID: {0}", Thread.CurrentThread.ManagedThreadId));
        }

        /// <summary>
        /// Returns a configuration section that matches the sectionName.
        /// </summary>
        /// <param name="sectionName">Name of the required section.</param>
        /// <returns></returns>
        public object GetSection(string sectionName)
        {
            SC.ExeConfigurationFileMap map = new SC.ExeConfigurationFileMap();
            map.ExeConfigFilename = ConfigurationUtils.GetSiblingConfigFilePath("mockingbird.config");

            if (!File.Exists(map.ExeConfigFilename))
            {
                throw new FileNotFoundException("Could not locate MockingBird config file in the path :" + map.ExeConfigFilename);
            }

            System.Configuration.Configuration config = SC.ConfigurationManager.OpenMappedExeConfiguration(map, SC.ConfigurationUserLevel.None);

            if (config == null)
            {
                throw new ConfigurationErrorsException(
                    String.Format(
                        "The specified MockingBird config file {0} does not have any unity related config sections",
                        map.ExeConfigFilename));
            }


            return config.GetSection(sectionName);
        }

        public virtual string GetSectionXml(string sectionName)
        {
            ConfigurationSection targetSection = (ConfigurationSection)this.GetSection(sectionName);
            return targetSection.SectionInformation.GetRawXml();
        }


    }
}
